#include"jiajia_lmcc.h"

extern void JiaJia_item_meminit(void *pt, size_t size){
    JiaJia_item_pt jjitem = (JiaJia_item_pt)pt;

    jjitem->item = _JiaJiaLmcc_item_lmcc;
    jjitem->word_num = 0;
    jjitem->word_list = NULL;
    jjitem->next = NULL;
}

extern void JiaJia_item_memfree(void *pt, size_t size){
    JiaJia_item_pt jjitem = (JiaJia_item_pt)pt;

    jjitem->item = _JiaJiaLmcc_item_lmcc;
    jjitem->word_num = 0;
    jjitem->word_list = NULL;

    if(jjitem->next != NULL)
        Yu_free(jjitem->next);

    jjitem->next = NULL;
}

extern JiaJiaLmcc_pdata_t *JiaJia_next_pdata(){
    JiaJia_file_pt jjfile = JiaJia_file_top();
    if(jjfile->pdata_top == NULL)
        JiaJia_file_push_pdata();

    return jjfile->pdata_top;
}

extern JiaJia_word_pt JiaJia_reduce_next_word(){
    JiaJia_file_pt jjfile = JiaJia_file_top();

    JiaJia_word_pt ret = jjfile->word_reduce;
    if(ret != NULL)
        jjfile->word_reduce = ret->next;
    return ret;
}

extern void JiaJia_reduce_push_item(int16_t item, JiaJia_word_pt word_list, int32_t word_num){
    JiaJia_file_pt jjfile = JiaJia_file_top();

    JiaJia_item_pt jjitem = Yu_alloc(sizeof(JiaJia_item_t), JiaJia_item_meminit, JiaJia_item_memfree);

    jjitem->item = item;
    jjitem->word_num = word_num;
    jjitem->word_list = word_list;
    jjitem->next = NULL;

    if(jjfile->item_reduce != NULL)
        jjitem->next = jjfile->item_reduce->next;

    jjfile->item_reduce = jjitem;
}

extern JiaJia_item_pt JiaJia_reduce_pop_item(){
    JiaJia_file_pt jjfile = JiaJia_file_top();

    JiaJia_item_pt ret = jjfile->item_reduce;
    if(ret != NULL){
        jjfile->item_reduce = ret->next;
        ret->next = NULL;
    }

    return ret;
}

extern int8_t JiaJia_reduce(){
    return 1;
}

extern int8_t JiaJia_reduce_constant(){
    JiaJia_word_pt const_word = JiaJia_reduce_next_word();

    /* 检查常量的合法性 */
    if(const_word->word == _JiaJiaLmcc_word_str){

    }

    JiaJia_reduce_push_item(_JiaJiaLmcc_item_constant, const_word, 1);
    return 1;
}
